package com.creative.accountsdk.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.creative.accountsdk.R;
import com.creative.accountsdk.bean.CourseInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 教师 选择任课 课程列表
 *
 *
 * @author  David
 * @version 1.0
 * @created 2015-11-25
 */
public class CourseAdapter extends BaseAdapter implements View.OnClickListener {

    private Context mContext;
    private  List<CourseInfo> list;
    public   List<Boolean> checkedList;

    private Callback mCallback;

    /***
     * 自定义接口，用于回调按钮点击事件到Activity
     */
    public interface Callback {
        public void click(View v);
    }

    public CourseAdapter(Context context, Callback callback) {
        this.mContext = context;
        this.mCallback = callback;
        this.checkedList = new ArrayList<>();
    }

    public void setData(List<CourseInfo> list) {
        this.list = list;
        for(int i=0;i<list.size();i++){
            checkedList.add(false);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int postion, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.teacher_course_adapter, null);

            viewHolder.tvName = (TextView) view.findViewById(R.id.tv_curse_name);
            viewHolder.ckAdd = (CheckBox) view.findViewById(R.id.ck_select_curse);
            viewHolder.vline = view.findViewById(R.id.v_cource_line);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.ckAdd.setOnClickListener(this);
        viewHolder.ckAdd.setTag(postion);

       final  CourseInfo info = list.get(postion);
        viewHolder.ckAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox cb = (CheckBox)v;
                checkedList.set(postion,cb.isChecked());
            }
        });

        viewHolder.tvName.setText(info.name);
        if(postion==list.size()-1){
            viewHolder.vline.setVisibility(View.GONE);
        }else{
            viewHolder.vline.setVisibility(View.VISIBLE);
        }
        return view;
    }

    @Override
    public void onClick(View v) {
        mCallback.click(v);
    }

    class ViewHolder {
        TextView tvName;
        CheckBox ckAdd;
        View   vline ;
    }
}